package com.hexiang.web;

import com.hexiang.domain.User;
import com.hexiang.handler.UserHandler;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@Api(tags = "用户数据接口(webflux)")
public class UserWebFluxController {

    @Autowired
    @Qualifier("userHandler")
    private UserHandler userHandler;

    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    @GetMapping("/webflux/users")
    public Flux<User> getUsers() {
        return userHandler.findAll();
    }

    @ApiOperation(value = "查询用户", notes = "根据 name 查询用户")
    @ApiImplicitParam(paramType = "path", name = "name", value = "用户名", required = true)
    @GetMapping("/webflux/user/**//{name}")
    public Mono<User> getUserByName(@PathVariable String name) {
        return userHandler.findByName(name);
    }

    @ApiOperation(value = "新增用户", notes = "根据传入的用户名和年龄添加一个新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "用户名", required = true, defaultValue = "test"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄", required = true, defaultValue = "123")})
    @PostMapping("/webflux/user")
    public Mono<Integer> addUser(@RequestParam String name, @RequestParam int age) {
        return userHandler.save(name, age);
    }

    @ApiOperation(value = "新增用户", notes = "根据传入的用户名和年龄添加一个新用户，返回用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "用户名", required = true, defaultValue = "test"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄", required = true, defaultValue = "123")})
    @PostMapping("/webflux/userReturn")
    public Mono<User> addUser(User user) {
        return userHandler.saveByUser(user);
    }

    @ApiOperation(value = "删除用户", notes = "根据 id 删除用户")
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功！"), @ApiResponse(code = 500, message = "删除失败！")})
    @DeleteMapping("/webflux/user/{id}")
    public Mono<Integer> deleteUserById(@PathVariable Long id) {
        return userHandler.delete(id);
    }

    @ApiOperation(value = "修改用户", notes = "传入用户信息进行更新修改")
    @PutMapping("/webflux/user")
    public Mono<Integer> updateUser(@Validated @RequestBody User user) {
        return userHandler.update(user);
    }

}